<?php
declare(strict_types=1);

namespace App\Middleware\Api;

use Hyperf\HttpServer\Contract\RequestInterface;
// use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Hyperf\Context\Context;
use App\Constants\Status;
use App\Common\RedisCache;
use Psr\Container\ContainerInterface;
use App\Exception\ApiException;
use App\Service\Common;
use App\Service\DataResource;

class ApiMiddleware implements MiddlewareInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var RequestInterface
     */
    protected $request;

    public function __construct(ContainerInterface $container, RequestInterface $request)
    {
        $this->container = $container;
        $this->request = $request;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $path = $this->request->path();
        if ($path === '/favicon.ico') {
            unset($path);
            throw new ApiException(Status::INVALID_API);
        }
        $appkey = $this->request->input('dev_key');
        // 必须有apkey
        if (empty($appkey)) {
            throw new ApiException(Status::CODE_FORBIDDEN, 'dev_key必须填写');
        }
        if (substr($appkey, 0, 4) !== 'DEV-') {
            $appkey = (new Common)->decryptDevKEY($appkey);
        }
        $this->checkSecure($appkey);
        // 判断apkey的正确性
        $this->checkPlatformAppkey($appkey);
        unset($appkey);
        return $handler->handle($request);
    }
    /**
     * 常规安全验证
     * @param string $appkey
     * @author QianLong <87498106@qq.com>
     * @version 1.0.0
     */
    public function checkSecure($appkey)
    {
        $bannedValue = redis()->get('dever_' . $appkey . '_FENGJIN');
        if ($bannedValue >= 1) {
            throw new ApiException(Status::CODE_FAIL, '用户被禁，如有疑问可咨询：87989431、87498106');
        }
    }
    /**
     * appkey真实性验证
     * @param string $appkey
     * @author QianLong <87498106@qq.com>
     * @version 1.0.0
     */
    public function checkPlatformAppkey(string $appkey)
    {
        $redisCacheObj = new RedisCache();
        $getUserSettingData = $redisCacheObj->getUserInfoByKey($appkey);
        if (empty($getUserSettingData)) {
            throw new ApiException(Status::NO_APPKEY);
        }
        $getUserSettingData['uid'] = (int)$getUserSettingData['uid'];
        $bannedValue = redis()->get('dever_FENGJIN:UID:' . $getUserSettingData['uid']);
        if ($bannedValue >= 1) {
            throw new ApiException(Status::CODE_FAIL, '用户被禁，如有疑问可咨询：87989431、87498106');
        }
        if ($getUserSettingData['api'] != 1) {
            throw new ApiException(Status::NO_ACCESS);
        }
        $userInfo = $redisCacheObj->getUserInfoById($getUserSettingData['uid']);
        unset($redisCacheObj);
        if ($userInfo['status'] != 1) {
            unset($userInfo);
            throw new ApiException(Status::USER_STATUS_ERROR);
        }
        Context::set('userInfo', $getUserSettingData);
        unset($getUserSettingData);
    }
}
